package com.zw.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zw.common.entity.product.BrandEntity;
import com.zw.common.entity.product.CategoryBrandRelationEntity;
import com.zw.common.utils.BeanUtils;
import com.zw.gulimall.product.vo.BrandsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zw.gulimall.product.service.CategoryBrandRelationService;
import com.zw.common.utils.PageUtils;
import com.zw.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author zw
 * @email Alisinngai@gmail.com
 * @date 2023-06-11 23:54:02
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 通过brandId列表
     */
    @RequestMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R cateLogList(@RequestParam(value = "brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.listByBrandId(brandId);
        return R.ok().put("data", data);
    }

//    /product/categorybrandrelation/brands/list
    @GetMapping("/brands/list")
    public R  BrandsList(@RequestParam("catId") Long catId){
        List<BrandEntity> brandEntityns = categoryBrandRelationService.queryBrandsByCatId(catId);
//        List<BrandsVo> brandsVos = BeanUtils.copyListProperties(brandEntityns, BrandsVo::new);
        List<BrandsVo> collect = brandEntityns.stream().map(item -> {
            BrandsVo brandsVo = BeanUtils.copyProperties(item, BrandsVo::new);
            brandsVo.setBrandName(item.getName());
            return brandsVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
